package com.htl.controller;

import com.htl.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.*;

@RestController
public class StudentController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *  增删改查
     */
    @PostMapping("/set")
    public void set(@RequestBody Student student){
        this.redisTemplate.opsForValue().set("stu", student);
    }

    @GetMapping("/get/{key}")
    public Student get(@PathVariable("key") String key){
        return (Student) this.redisTemplate.opsForValue().get(key);
    }

    @PutMapping("/put")
    public void update(@RequestBody Student student){
        this.redisTemplate.opsForValue().set("stu", student);
    }

    @DeleteMapping("/delete/{key}")
    public Boolean delete(@PathVariable("key") String key){
        this.redisTemplate.delete(key);
        return this.redisTemplate.hasKey(key);
    }



    /**
     *  5种数据类型
     */
    @PostMapping("/string")
    public String string(){
        String str = "Hello World";
        this.redisTemplate.opsForValue().set("str", str);
        return (String) this.redisTemplate.opsForValue().get("str");
    }

    @PostMapping("/list")
    public void list(){
        ListOperations<String,String> list = redisTemplate.opsForList();
        list.leftPush("list", "Hello");
        list.leftPush("list", "World");
        list.leftPush("list","Java");
        list.rightPush("list", "1");
        list.rightPush("list", "2");
        list.rightPush("list", "3");
    }

    @PostMapping("/setadd")
    public void setadd(){
        SetOperations<String,String> set = this.redisTemplate.opsForSet();
        set.add("set", "Hello");
        set.add("set", "World");
        set.add("set", "Java");
    }

    @PostMapping("/zset")
    public void zset(){
        ZSetOperations<String,String> set = this.redisTemplate.opsForZSet();
        set.add("zset", "Hello",1);
        set.add("zset", "World",2);
        set.add("zset", "Java",3);
    }

    @PostMapping("/hash")
    public void hash(){
        HashOperations<String,String,String> hash = this.redisTemplate.opsForHash();
        hash.put("hash", "id", "1");
        hash.put("hash", "name", "tom");
        hash.put("hash", "age","22" );
    }


}

